package main

import (
	"fmt"

	"gitee.com/shexk/gtl/core"
	x "gitee.com/shexk/gtl/protocol"
)

type KeyValue struct {
	Key   uint64
	Value string
}

// 定义示例结构体
type TransFileInfo struct {
	Command  string //GetDir或Download
	Dir      string
	FileName []string //路径信息或文件名称
	Number   uint     //子路径数量
	BDir     []byte   //0x00开始，0x01传送数据，0x02传送结束 是否文件夹
	Length   []uint   //ID或文件大小
}

func main() {
	info := []TransFileInfo{{Command: "DownloadFile", Dir: "/mydata/abc", Number: 3,
		FileName: []string{"/mydoc/abc.go", "/mydoc/adf.c", "/mydoc/cc.a"},
		BDir:     []byte{},
		Length:   []uint{1212, 2323, 343434}},
		{Command: "GetDir", Dir: "/mydata/abc", Number: 3,
			FileName: []string{"/mydoc/abc.go", "/mydoc/adf.c", "/mydoc/cc.a"},
			BDir:     []byte{},
			Length:   []uint{1212, 2323, 343434}}}

	abc, _ := core.EncoderAnyDataToBytes(info)
	fmt.Println(len(abc))
	dd := make([]TransFileInfo, 2)
	core.DecoderBytesToAnyData(abc, &dd)
	fmt.Println(dd)

	a1, _ := core.EncoderAnyDataToBytes([]string{"adasf", "12233"})

	a2 := make([]string, 3)
	core.DecoderBytesToAnyData(a1, &a2)
	fmt.Println(a2)

	for i := 0; i < 2; i++ {
		abcd, _ := x.GetBytesFromStruct(info[i])
		fmt.Println("Length:", len(abcd))
		cc := TransFileInfo{}
		x.GetStructFromBytes(abcd, &cc)
		fmt.Println(cc)
	}

	buf, _ := x.GetBytesFromKeyValue(2324, []string{"123", "456", "112"})

	k, v, _ := x.GetKeyValueFromBytes(buf)
	ss := v.([]string)
	fmt.Println(k, ss)

	asd, _ := x.GetBytesFromValue(true)
	vv, _ := x.GetValueFromBytes(asd)
	fmt.Println(vv)

	// // 2. 序列化：使用 bytes.Buffer 作为内存载体
	// var bufs bytes.Buffer
	// encoder := gob.NewEncoder(&bufs) // 基于 buf（io.Writer）创建编码器
	// if err := encoder.Encode(info); err != nil {
	// 	panic("encode failed: " + err.Error())
	// }
	// // 此时 buf 中存储的是 gob 格式的二进制数据
	// fmt.Printf("Serialized data (len: %d)\n", bufs.Len())

	// // 3. 反序列化：从 buf 中读取数据并解析为 User 类型
	// var decodedUser TransFileInfo
	// decoder := gob.NewDecoder(&bufs) // 基于 buf（io.Reader）创建解码器
	// if err := decoder.Decode(&decodedUser); err != nil {
	// 	panic("decode failed: " + err.Error())
	// }
	// // 验证反序列化结果
	// fmt.Printf("Decoded data: %+v\n", decodedUser)
	// fmt.Println(decodedUser)

}
